﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Axis.Dao.Research;
using Axis.BusinessObject.Research;
using Axis.Web.Administrator.Global;
using System.Text;
using System.Xml;

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Axis.BusinessObject.Core;
using System.Web.Script.Serialization;
using System.IO;

namespace Axis.Web.Administrator.Views.Research
{
    public class ResearchUI : System.Web.UI.Page
    {
        CResearchDao cResearchDao = null;
        public ResearchUI()
        {
            cResearchDao = new CResearchDao();
        }
        protected IList<CProject> GetProject(string inputValue)
        {
            IList<CProject> list = cResearchDao.GetProject(inputValue);
            return list;
        }
        public string GetProjectList()
        {
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;

            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("UserID", member.ID.ToString());
            root.SetAttribute("SecrectToken", member.SecrectToken);
            root.SetAttribute("Type", "1");
            root.SetAttribute("Action", "GET");
            root.SetAttribute("PageIndex", "1");
            root.SetAttribute("RowsPerPage", "999");
            CResearchDao core = new CResearchDao();
            CCore cCore = new CCore();
            string inputValue = root.OuterXml;
            return GetProjectListHTML(inputValue);
        }

        public string GetProjectListHTML(string inputValue)
        {
            StringBuilder ret = new StringBuilder();
            IList<CProject> list = GetProject(inputValue);
            if (list != null && list.Count > 0)
            {
                foreach (CProject project in list)
                {
                    ret.Append(GetProjectHTML(project));
                    ret.Replace("[RowNumber]", list.IndexOf(project).ToString());
                    
                }
            }
            return ret.ToString();
        }
        private Random random = new Random();
        protected string GetProjectHTML(CProject project)
        {
            StringBuilder ret = new StringBuilder();
            if (project != null)
            {
                int id = project.ID;
                string name = project.Name == null ? "" : project.Name;
                string desc = "";
                if (project.Description != null)
                    desc = project.Description;
                string image1 = project.Image1 == null ? "" : project.Image1;
                var html = GetProjectTemplate();
                string[] contentTypeList = new string[] {"network","people","work","careers" };
                string contentType = contentTypeList[random.Next(0, contentTypeList.Length - 1)];
                html = html.Replace("[ContentType]", contentType);
                html = html.Replace("[ID]", id.ToString());
                html = html.Replace("[Image]", image1);
                html = html.Replace("[Name]", name);
                html = html.Replace("[Description]", desc);
                ret.Append(html);
            }
            return ret.ToString();
        }
        protected string GetProjectTemplate()
        {
            string ret = string.Empty;
            StreamReader streamReader = File.OpenText(HttpContext.Current.Server.MapPath("~/Views/template/Research_list.txt"));
            ret = streamReader.ReadLine();
            while (streamReader.ReadLine() != null)
            {
                ret += streamReader.ReadLine();
            }
            streamReader.Close();
            return ret;
        }

      

    }
}